﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Win104.Common;

namespace Win104.IEC104
{
    public class Asdu
    {
        //功能类型
        private int TypeId;
        //是否连续
        private bool IsSequenceOfElements;
        //可变结构限定词，第7位是否连续
        private int VSQ;
        //可变结构限定词，数据的数量
        private int AddressNum;
        //传输原因
        private int CauseOfTransmission;
        private bool Test;
        private bool NegativeConfirm;
        private int OriginatorAddress;
        private int CommonAddress;
        private InformationObject[] informationObjects;
        private byte[] PrivateInformation;

        public Asdu(ByteBody data)
        {
            //类型
            this.TypeId = data.GetByte() & 0xFF;
            Console.WriteLine("类型标识：" + TypeId);
            if (!Init.TypeHash.ContainsKey(TypeId))
                Console.WriteLine("无效的类型标识：" + TypeId);
            else
                Console.WriteLine("类型标识：" + TypeId);

            int vsqNum = data.GetByte() & 0xFF;
            string vsqFormat = ConvertHelper.TenToBinary(vsqNum);
            //可变结构限定词，转换为二进制获取第8位，信息体是否连续
            VSQ = int.Parse(vsqFormat.Substring(0, 1));
            //可变结构限定词，获取第1到7位，代表数据的数量
            AddressNum = ConvertHelper.BinaryToTen(vsqFormat.Substring(1, 7));
            if(VSQ == 1)
            {
                IsSequenceOfElements = true;
                Console.WriteLine("信息体地址连续：" + IsSequenceOfElements + "，信息数据量：" + AddressNum);
            }
            else
            {
                IsSequenceOfElements = false;
                Console.WriteLine("信息体地址连续：" + IsSequenceOfElements + "，信息数据量：" + AddressNum);
            }

            int numberOfSequenceElements;
            int numberOfInformationObjects;
            //根据是否连续来判断信息对象数目、信息元素数目
            if (IsSequenceOfElements)
            {
                //信息数据数目
                numberOfSequenceElements = AddressNum;
                //信息元素数目为1（地址）
                numberOfInformationObjects = 1;
            }
            else
            {
                //信息数据数目
                numberOfSequenceElements = 1;
                //信息元素数目
                numberOfInformationObjects = AddressNum;
            }

            //传送原因，暂时先这样
            byte[] cause = data.GetBytes(2);
            CauseOfTransmission = cause[0];
            Console.WriteLine("传送原因：" + CauseOfTransmission);

            //公共地址
            byte[] bAddress = data.GetBytes(2);
            CommonAddress = ConvertHelper.HexToTen(ConvertHelper.ByteToHex(bAddress[1]) + ConvertHelper.ByteToHex(bAddress[0]));
            Console.WriteLine("公共地址：" + CommonAddress);

            //信息体
            if(TypeId < 128)
            {
                informationObjects = new InformationObject[numberOfInformationObjects];
                for (int i = 0; i < numberOfInformationObjects; i++)
                {
                    informationObjects[i] = new InformationObject(data, TypeId, numberOfSequenceElements);
                }
                PrivateInformation = null;
            }
            else
            {
                Console.WriteLine(" ");
            }
        }

        public int GetTypeId() { return TypeId; }
        public InformationObject[] GetInformationObjects() { return informationObjects; }





    }
}
